
/**
 * 初始化上传插件
 */
var pathname = window.location.pathname;
//alert(pathname);
var uploadErrorMsg = null;
$("#add_pic").uploadify({
    'auto': false,
    'method':'post',
    'swf': pathname+'resources/plugins/uploadify/uploadify.swf',
    'uploader': 'upload/uploadTrackingRecordImage',  //后台处理程序的相对路径
    'buttonText': '选择文件',
    'fileTypeDesc':'不超过100k的图片 (*.gif;*.jpg;*.png)',
    'fileSizeLimit': '100KB', //允许上传的文件大小(kb)  此为100k
    'fileTypeExts':'*.gif;*.jpg;*.png',
    'width':60,//浏览按钮的宽和高
    'height':24,
    'width':65,//浏览按钮的宽和高
    'height':25,
    'formData': {},
    'overrideEvents' : ['onDialogClose', 'onSelectError'],
    'onUploadError':function(file, errorCode, errorMsg, errorString){
    	$.messager.alert('消息',"图片" + file.name + " 上传失败！");
    },
    'onUploadSuccess': function(file, data, response) {//当单个文件上传完成后触发
    	//$('#addTrackingRecordForm').form('clear');
		$('#trackingRecordGrid').datagrid('load');
    	/*$.messager.alert('消息',"图片" + file.name + " 上传成功！");*/
    },
    'onDialogClose' : function(filesSelected, filesQueued, queueLength) {
    	if (this.queueData.filesErrored > 0) {
    		$.messager.alert('消息',"图片" + uploadErrorMsg + " 取消上传！");
		}
    },
    'onSelectError' : function(file, errorCode, errorMsg) {
    	var settings = this.settings;
    	switch(errorCode) {
			case SWFUpload.QUEUE_ERROR.QUEUE_LIMIT_EXCEEDED:
				uploadErrorMsg = '\n文件数量超过上传限制 ';
				break;
			case SWFUpload.QUEUE_ERROR.FILE_EXCEEDS_SIZE_LIMIT:
				uploadErrorMsg = '\n文件 "' + file.name + '" 超过大小限制 (' + settings.fileSizeLimit + ')';
				break;
			case SWFUpload.QUEUE_ERROR.ZERO_BYTE_FILE:
				uploadErrorMsg = '\n文件 "' + file.name + '" 大小为0';
				break;
			case SWFUpload.QUEUE_ERROR.FILE_EXCEEDS_SIZE_LIMIT:
				uploadErrorMsg = '\n文件 "' + file.name + '" 类型不接受 (' + settings.fileTypeDesc + ')';
				break;
		}
    }
});

function bindData(rowData){
	var unemployedId = rowData.unemployedId;
//	alert(unemployedId);
	$('#unemployedId').val(unemployedId);
	if(rowData.knry!='1'){
		$('#addTrackingRecordButton').linkbutton('disable');
		$('#updateTrackingRecordButton').linkbutton('disable');
		$('#delTrackingRecordButton').linkbutton('disable');
	} else if(rowData.knry=='1'){
		$('#addTrackingRecordButton').linkbutton('enable');
		$('#updateTrackingRecordButton').linkbutton('enable');
		$('#delTrackingRecordButton').linkbutton('enable');	
	}

    $('#trackingRecordGrid').datagrid({ 
    	url:'jobless/difficultyServer/getTrackingRecordList',  
        idField:'id',
        queryParams:{
        	unemployedId: unemployedId
	    },
        rownumbers:true,
        fit:true,
        fitColumns:true,
        //columnfit:true,
        singleSelect:true,
        pagination:true,
        
        columns:[[
                  {field:'id',hidden:'true'}, 
                  {field:'unemployedId',hidden:'true'},
                  {field:'interviewdate',title:'走访时间',width:100,align:'center',
                	     formatter:function(data){ 
                	    	 if(data!=null){
                	    		 var df = new Date(data);  
                                 return df.format("yyyy-MM-dd");       			   
       		                 } else{
       		        	         return null;
       		                 }
       	                 }},  
                  {field:'theme',title:'走访主题',width:100,align:'center'},
                  {field:'location',title:'走访地点',width:100,align:'center'},
                  {field:'num',title:'人数',width:100,align:'center'},                 
                  {field:'maincontent',title:'走访跟踪主要内容',width:100,align:'center'},
                  {field:'who',title:'跟踪对象',width:100,align:'center'},
                  {field:'pic',title:'走访记录',width:100,align:'center',
                	formatter:function(data,row){
                		if(data!=null){
                			return '<a href="#" onclick=window.open("upload/selectFile?id='+row.id+'")>下载记录<a>';
                		}
                	}  
                  }                
                ]]    	
    }); 
}

$("#addTrackingRecord").window({
	onClose:function(data){
		$('#addtrackingRecordForm').form('clear');
	}  		      
});

function addTrackingRecord(){
	var curr_time = new Date();
	var strDate = curr_time.getFullYear()+"-"+(curr_time.getMonth()+1)+"-"+curr_time.getDate();
	$('#trackingRecordAdd_interviewdate').datebox('setValue',strDate);	
	$('#addTrackingRecord').window('open');
}

/**
 * 添加困难人员跟踪服务记录
 */
function insertTrackingRecord(){
	//alert($('#unemployedId').val());
	$('#addtrackingRecordForm').form('submit',{
		url:'jobless/difficultyServer/insertTrackingRecord',
		queryParams:{
			unemployedId: $('#unemployedId').val()
		},
		success: function(data){
			data =  $.parseJSON(data);
			if(data.success == 'true'){
				var id = data.rows.id;
				$("#add_pic").uploadify("settings","formData",{"id":id});
				$('#add_pic').uploadify('upload');
				$.messager.alert('提示','保存成功','info');
				$('#addTrackingRecord').window('close');
				//$('#addTrackingRecordForm').form('clear');
				$('#trackingRecordGrid').datagrid('load');
			}else{
				$.messager.alert('提示',data.message,'info');
			}
		}
	});
	
}

/**
 * 删除一条添加困难人员跟踪服务记录
 * 
 */
function delTrackingRecord(){
	var rowData = $('#trackingRecordGrid').datagrid('getSelected');
	if(rowData == undefined){
		$.messager.alert('消息','请选择一条数据');
	}else{
		$.messager.confirm('提示','确定要删除吗？',function(r){
			if(r){
				$.ajax({
					url:'jobless/difficultyServer/deleteTrackingRecord',
					type:'post',
					data:{
						id:rowData.id
					},
					success:function(data){
						data =  $.parseJSON(data);
						if(data.success == 'true'){
							$.messager.alert('提示','删除成功','info');
							$('#trackingRecordGrid').datagrid('reload');
						}else{
							$.messager.alert('提示',data.message,'info');
						}
					}
				});
			}
		});
	}
}

/**
 * 回显一条困难人员跟踪服务记录
 * 
 */
function getTrackingRecord(){
	var rowData = $('#trackingRecordGrid').datagrid('getSelected');
	if(rowData == undefined){
		$.messager.alert('消息','请选择一条数据');
	}else{
		$('#modifyTrackingRecord').window('open');
		//alert(rowData.id);
		$.ajax({
			url:'jobless/difficultyServer/getTrackingRecordInfo',
			type:'post',
			data:{
				id:rowData.id
			},
			success: function (data) {
				if(data==null){
					$('#modifyTrackingRecordForm').form('clear');
				}else{
					$('#modifyTrackingRecordForm').form('load',data);
				}
	        },
	        error:function(data){
	        	$('#modifyTrackingRecordForm').form('clear');
	        }
		});
	}
}

function modifyTrackingRecord(){
	var modifyFlag = $('#modifyTrackingRecordForm').form('validate');
	if(modifyFlag==false){
		$.messager.alert('提示','表单填写不正确');
		return;
	}else{
		$.ajax({
			url:'jobless/difficultyServer/updateTrackingRecord',
			type:'post',
	        async: false,
	        data:$('#modifyTrackingRecordForm').serialize(),
	        success:function(data){
	        	data =  $.parseJSON(data);
		        if(data.success == 'true'){
		        	$.messager.alert('提示','保存成功','info');
			        $('#modifyTrackingRecord').window('close');
			        $('#modifyTrackingRecordForm').form('clear');
			        $('#trackingRecordGrid').datagrid('reload');
					$('#trackingRecordGrid').datagrid('unselectAll');
		        }else{
		        	$.messager.alert('提示',data.message,'info');

		        }
	        },
	        error:function(data){
	        	alert("ajax发生错误！");	
	        }
        });
	}	
}

/**
 * 日期格式化
 * @param value
 * @param row
 * @returns {String}
 */
function formatterDate(value) {
    var date = new Date(value);
    return date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate();
}

function parserDate(s){
	//alert("parserDate");
	var t = Date.parse(s);
	if (!isNaN(t)){
		return new Date(t);
	} else {
		return new Date("1975-08-01");
	}
	
    /*if(!s) return new Date();
	var ss = (s.split('-'));
    var y = parseInt(ss[0],10);
    var m = parseInt(ss[1],10);
    var d = parseInt(ss[2],10);
	if (!isNaN(y) && !isNaN(m) && !isNaN(d)){
		return new Date(y,m-1,d);
	}else{
		return new Date();
	}*/
	
}